/*
 *  CombatScene.cpp
 *  SpaceTrader
 *
 *  Created by Jake on 10/6/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include "GuiButton.h"
#include "CombatScene.h"
#include "Ship.h"
#include "Laser.h"

CombatScene::CombatScene() : shipHit(false), timeCountAfterHit(0){
}

/*
 * TODO:Rework combat scene and incorporate OpenGl to improve gameplay
 *
 */
state_t CombatScene::draw(SDL_Surface *screen, Player *player)
{
	TTF_Font *buttonFont = TTF_OpenFont( "Futura.ttc", 20 );
	SDL_Surface *background1 = load_image("starfield.png",false);
	SDL_Surface *background2 = load_image("starfield2.png",false);
	int scrollSpeed = 2;
	int x1 = 0;
	int x2 = 0;

	
	SDL_Event event;
	state_t next_state = COMBAT;
	//Initialize Objects
	Ship testShip(1,1,1,1,"def_ship2");
	Ship enemyShip(1,1,1,1,3,"def_ship3");
	enemyShip.setX(480);
	
	while (next_state==COMBAT) {
		
		//Event Loop
		while (SDL_PollEvent(&event)) {
			//Buttons
			if (event.type==SDL_QUIT) next_state = QUIT;
			
			//Ship
			testShip.handle_input(event);
			
		}
		//AI ship logic

		//Dodge
		/*
		for ( int i=0; i < testShip.getLasers().size(); i++) {
			if (testShip.getLasers()[i]->getX() + testShip.getLasers()[i]->getW()
					< enemyShip.getX() + 50 && testShip.getLasers()[i]->getY() >
					enemyShip.getY() && testShip.getLasers()[i]->getY() <
					enemyShip.getY() + enemyShip.getH()) {
				if (testShip.getLasers()[i]->getY() > enemyShip) {

				}
			}
		}
		*/
		//



		//

		testShip.move();
        enemyShip.move();
        
        if (enemyShip.getY() <= 10){
            enemyShip.setYVel(3);
        } else if (enemyShip.getY() >= 380){
            enemyShip.setYVel(-3);
        }
		
		if(SDL_GetTicks() < 1000 / FPS)
			SDL_Delay((1000/FPS)-SDL_GetTicks());

		//Apply objects
		SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
		if (x1 <= (-1) * SCREEN_WIDTH) x1 = 0;
		if (x2 <= (-1) * SCREEN_WIDTH) x2 = 0;
		apply_surface( x1, 0, background1, screen );
		apply_surface( x2, 0, background2, screen );
		apply_surface( x1 + SCREEN_WIDTH, 0, background1, screen );
		apply_surface( x2 + SCREEN_WIDTH, 0, background2, screen );
		x1 -= scrollSpeed;
		x2 -= (scrollSpeed / 2);
		testShip.show(screen);
        enemyShip.show(screen);

		std::vector<Laser*> lasers = testShip.getLasers();
		for (unsigned int i = 0; i < lasers.size(); i++) {
			lasers[i]->show(screen);
			lasers[i]->move();
		}
        
        std::vector<Laser*> enemyLasers = enemyShip.getLasers();
		for (unsigned int i = 0; i < enemyLasers.size(); i++) {
			enemyLasers[i]->show(screen);
			enemyLasers[i]->move();
		}
        
        if (enemyShip.checkIfHit(lasers) || testShip.checkIfHit(enemyLasers)) {
            player->getShip()->setFuel(0);
            shipHit = true;
        }
        
        if (shipHit) {
            timeCountAfterHit++;
        }
        
        if (timeCountAfterHit > 100){
            shipHit = false;
            timeCountAfterHit = 0;
            next_state = TRAVEL;
        }
        
		testShip.setLasers(lasers);
        if (std::rand()%100 > 97){
            enemyShip.shoot();
        }
		//Update the screen
		if ( SDL_Flip( screen ) == -1 ) {
			std::cerr << "Error Updating screen." << std::endl;
		}
	}
	SDL_FreeSurface(background1);
	SDL_FreeSurface(background2);
	TTF_CloseFont(buttonFont);
	return next_state;
}
